package net.godcode.olivenotes.components;

import net.godcode.olivenotes.services.security.UserAuthenticator;

import org.apache.tapestry.Block;
import org.apache.tapestry.annotations.BeforeRenderBody;
import org.apache.tapestry.annotations.BeginRender;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.ioc.annotations.Inject;

/**
 * IfLoggedIn is part of the security subsystem.
 * 
 * See: <a href="http://www.localhost.nu/java/tapestry5-acegi/">tapestry5-acegi</a>
 * 
 * @author Chris Lewis Jan 5, 2008 <chris@thegodcode.net>
 * @version $Id: IfLoggedIn.java 32 2008-01-22 15:14:31Z burningodzilla $
 */
public class IfLoggedIn {
	
	/**
	 * The alternate block of content that will be rendered if the normal
	 * body cannot be.
	 */
	@Parameter(name = "else")
	private Block elseBlock;
	
	@Inject
	private UserAuthenticator authenticator;
	
	@BeginRender
	Object beginRender() {
		if(authenticator.isLoggedIn()) {
			return null;
		} else {
			return elseBlock;
		}
	}
	
	@BeforeRenderBody
	boolean beforeRenderBody() {
		return authenticator.isLoggedIn();
	}
	
}